package view;

import control.KeyAction;
import control.MouseAction;
import model.Model;

import javax.swing.*;
import javax.swing.border.BevelBorder;

import static model.Model.data;
import static model.Model.victory;
import static model.Model.images;
import static model.Model.num;
import static model.Model.choose;


public class GameFrame extends JFrame {
    Model model=new Model(this);

    private  JMenuItem Figure = new JMenuItem("表情包");

    private JMenuItem Sheep = new JMenuItem("小羊");

    private JMenuItem SpongeBob = new JMenuItem("海绵宝宝");
    //重新登录项
    private  JMenuItem replay = new JMenuItem("重新游戏");
    //重新游戏项
    private  JMenuItem logout = new JMenuItem("重新登录");
    //关闭游戏项
    private  JMenuItem exit = new JMenuItem("关闭游戏");

    private  JMenuItem author = new JMenuItem("关于我们");
    public static int step;
    KeyAction keyAction=new KeyAction(this,model);

    MouseAction mouseAction=new MouseAction(this,model);

    public GameFrame() {
        //初始化窗体
        initJFrame();
        //初始化菜单
        initJMenu();
        //判断是否胜利
        model. isVictory();
        //初始化数据
        model.initData();
        //初始化图片
        initImg();
        //初始化事件监听
        addListener();
        //显示界面
        setVisible(true);
    }
    public void initJFrame() {
        //设置窗体大小
        setSize(603, 680);
        //设置窗体标题
        setTitle("拼图小游戏 v1.0");
        //设置窗体的相对位置
        setLocationRelativeTo(null);
        //设置
        setAlwaysOnTop(true);
        //取消窗体的默认布局
        setLayout(null);
        //设置窗体的关闭方式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //给整个窗体添加键盘监听事件
        addKeyListener(keyAction);
    }
    public void initJMenu() {
        //创建总菜单
        JMenuBar menuBar = new JMenuBar();
        //创建功能菜单
        JMenu function = new JMenu("功能");
        //创建更换图片菜单
        JMenu reImg = new JMenu("更换图片");

        reImg.add(Figure);
        reImg.add(Sheep);
        reImg.add(SpongeBob);

        function.add(reImg);
        function.add(replay);
        function.add(logout);
        function.add(exit);
        //创建"关于我们"菜单
        JMenu about = new JMenu("关于我们");
        about.add(author);
        menuBar.add(function);
        menuBar.add(about);
        setJMenuBar(menuBar);
    }
    public  void initImg() {
        //删除窗体，重新创建窗体
        getContentPane().removeAll();
        //判断是否胜利,设置胜利的图标
        if (victory) {
            JLabel win = new JLabel(new ImageIcon(images + "win.png"));
            win.setBounds(203, 283, 197, 73);
            getContentPane().add(win);
        }
        //记录步数标签
        addStep();
        String path = images + choose + num;
        //通过两层for循环生成图片
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                //创建图片标签
                JLabel img = new JLabel(new ImageIcon(path + data[i][j] + ".jpg"));
                //设置图片标签的位置和大小
                img.setBounds(j * 105 + 83, i * 105 + 134, 105, 105);
                //设置图片标签的边框
                img.setBorder(new BevelBorder(BevelBorder.LOWERED));
                //将图片加入窗体
                getContentPane().add(img);
            }
        }
        //背景图片生成
        addBackground();
       repaint();
    }
    public void addListener() {
        mouseAction.setFigure(Figure);
        mouseAction.setSheep(Sheep);
        mouseAction.setSpongeBob(SpongeBob);
        mouseAction.setReplay(replay);
        mouseAction.setLogout(logout);
        mouseAction.setExit(exit);
        mouseAction.setAuthor(author);
        //给各项绑定事件监听
        Figure.addActionListener(mouseAction);
        Sheep.addActionListener(mouseAction);
        SpongeBob.addActionListener(mouseAction);
        replay.addActionListener(mouseAction);
        logout.addActionListener(mouseAction);
        exit.addActionListener(mouseAction);
        author.addActionListener(mouseAction);
    }
    public void addStep() {
        //创建记录步数的标签
        JLabel stepCount = new JLabel("步数: " + step);
        //设置步数标签的位置和大小
        stepCount.setBounds(50, 30, 100, 20);
        //将标签放进窗体中
        getContentPane().add(stepCount);
    }
    public void addBackground() {
        //创建背景图片标签
        JLabel bgi = new JLabel(new ImageIcon(images + "background.png"));
        //设置背景图片标签的位置和大小
        bgi.setBounds(40, 40, 508, 560);
        //将标签放进窗体中
        getContentPane().add(bgi);
    }


}
